package savet.gui.dialogos;

import savet.gui.tabelas.DateCellRenderer;
import java.util.Date;
import java.util.LinkedList;
import java.util.List;
import savet.entidades.atributos.Periodo;
import savet.util.JOptionUtil;
import savet.util.SauditaLog;

/**
 *
 * @author Adisson Marinho
 */
public class DialogoListaPeriodos extends javax.swing.JDialog {

    private Periodo periodo;
    private int selectedRow = -1;
    private boolean editing = false;

    /** Creates new form DialogoListaFerias */
    public DialogoListaPeriodos(java.awt.Frame parent, boolean modal) {
        this(parent, modal, new LinkedList<Periodo>());
    }

    /** Creates new form DialogoListaFerias */
    public DialogoListaPeriodos(java.awt.Frame parent, boolean modal, List<Periodo> periodos) {
        super(parent, modal);
        initComponents();
        configurarJTable();
        
        // criando data para periodo de teste
        // Date date = new Date();
        // periodos.add(new Periodo(date, date));
        modeloListaPeriodos.setPeriodos(periodos);
        setLocationRelativeTo(null);
    }

    public final void configurarJTable() {
        tablePeriodos.setDefaultRenderer(Date.class, new DateCellRenderer());
    }

    private Periodo getPeriodoCampos() {
        Date di = dateInicio.getDate();
        Date df = dateFim.getDate();
        return new Periodo(di, df);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        modeloListaPeriodos = new savet.gui.modelos.ModeloListaPeriodos();
        painel = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        dateInicio = new org.openswing.swing.client.DateControl();
        dateFim = new org.openswing.swing.client.DateControl();
        deleteButton = new org.openswing.swing.client.DeleteButton();
        insertButton = new org.openswing.swing.client.InsertButton();
        editButton = new org.openswing.swing.client.EditButton();
        saveButton = new org.openswing.swing.client.SaveButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tablePeriodos = new javax.swing.JTable();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setResizable(false);

        painel.setBorder(javax.swing.BorderFactory.createTitledBorder("Periodos de gozo"));

        jLabel1.setText("De:");

        jLabel2.setText("Até:");

        deleteButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteButtonActionPerformed(evt);
            }
        });

        insertButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                insertButtonActionPerformed(evt);
            }
        });

        editButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                editButtonActionPerformed(evt);
            }
        });

        saveButton.setEnabled(false);
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        tablePeriodos.setModel(modeloListaPeriodos);
        jScrollPane1.setViewportView(tablePeriodos);

        javax.swing.GroupLayout painelLayout = new javax.swing.GroupLayout(painel);
        painel.setLayout(painelLayout);
        painelLayout.setHorizontalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 427, Short.MAX_VALUE)
                    .addGroup(painelLayout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(12, 12, 12)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(dateFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 41, Short.MAX_VALUE)
                        .addComponent(insertButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap())
        );
        painelLayout.setVerticalGroup(
            painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(painelLayout.createSequentialGroup()
                .addContainerGap(16, Short.MAX_VALUE)
                .addGroup(painelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addComponent(dateInicio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel1)
                    .addComponent(dateFim, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(saveButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(editButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(insertButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(deleteButton, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 192, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        getContentPane().add(painel, java.awt.BorderLayout.CENTER);

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void insertButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_insertButtonActionPerformed
        periodo = getPeriodoCampos();
        modeloListaPeriodos.adicionarPeriodo(periodo);
        resetarValores();
    }//GEN-LAST:event_insertButtonActionPerformed

    private void editButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_editButtonActionPerformed
        selectedRow = tablePeriodos.getSelectedRow();
        if (selectedRow != -1) {
            insertButton.setEnabled(false);
            deleteButton.setEnabled(false);
            editButton.setEnabled(false);
            saveButton.setEnabled(true);

            periodo = modeloListaPeriodos.getPeriodo(selectedRow);
            dateInicio.setDate(periodo.getDataInicio());
            dateFim.setDate(periodo.getDataFim());
            editing = true;
        } else {
            JOptionUtil.showWarningMessage("Selecione um periodo da tabela!", "Falta selecionar...");
        }
    }//GEN-LAST:event_editButtonActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        if (editing) {
            SauditaLog.adicionarInfoLog(getClass(), "saving...");
            Periodo temp = getPeriodoCampos();
            periodo.setDataInicio(temp.getDataInicio());
            periodo.setDataFim(temp.getDataFim());
            modeloListaPeriodos.setPeriodo(periodo);
            resetarValores();
            editing = false;

            insertButton.setEnabled(true);
            deleteButton.setEnabled(true);
            editButton.setEnabled(true);
            saveButton.setEnabled(false);
        }
    }

    private void resetarValores() {
        periodo = new Periodo();
        dateInicio.setValue(null);
        dateFim.setValue(null);
    }//GEN-LAST:event_saveButtonActionPerformed

    private void deleteButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteButtonActionPerformed
        selectedRow = tablePeriodos.getSelectedRow();
        if (selectedRow != -1) {
            modeloListaPeriodos.removerPeriodo(selectedRow);
        } else {
            JOptionUtil.showWarningMessage("Selecione um periodo da tabela!", "Falta selecionar...");
        }
    }//GEN-LAST:event_deleteButtonActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                DialogoListaPeriodos dialog = new DialogoListaPeriodos(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private org.openswing.swing.client.DateControl dateFim;
    private org.openswing.swing.client.DateControl dateInicio;
    private org.openswing.swing.client.DeleteButton deleteButton;
    private org.openswing.swing.client.EditButton editButton;
    private org.openswing.swing.client.InsertButton insertButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private savet.gui.modelos.ModeloListaPeriodos modeloListaPeriodos;
    private javax.swing.JPanel painel;
    private org.openswing.swing.client.SaveButton saveButton;
    private javax.swing.JTable tablePeriodos;
    // End of variables declaration//GEN-END:variables

}
